package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.mapper.BuyerExecuteDetailMapper;
import com.xt.purchasingsystem.mapper.QualityDetailMapper;
import com.xt.purchasingsystem.mapper.ReleaseCargoMapper;
import com.xt.purchasingsystem.mapper.SupplierFileMapper;
import com.xt.purchasingsystem.model.SupplierFile;
import com.xt.purchasingsystem.service.SupplierFileService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@Service
@Transactional
public class SupplierFileServiceImpl extends ServiceImpl<SupplierFileMapper, SupplierFile> implements SupplierFileService {
    @Autowired
    private SupplierFileMapper supplierFileMapper;
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BuyerExecuteDetailMapper buyerExecuteDetailMapper;
    @Autowired
    private ReleaseCargoMapper releaseCargoMapper;
    @Autowired
    private QualityDetailMapper qualityDetailMapper;

    @Override
    public Result getAllSupplierFileByProductName(String productName,Integer currNo,Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);

        List<SupplierFile> allSupplierFileByProductName = supplierFileMapper.getAllSupplierFileByProductName(productName);
        PageInfo<SupplierFile> pageInfo = new PageInfo<>(allSupplierFileByProductName);
        return ResultFactory.buildSuccessResult("查询成功",pageInfo);
    }

    @Override
    public Result getDataDrawingListBySupplierId(String supplierId) {

        LocalDate date = LocalDate.now();
        List<String> dates = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        List<Long> buyerSumAmounts = new ArrayList<>();
        List<Long> releaseCargoSumAmounts = new ArrayList<>();
        List<Long> qualitySumAmounts = new ArrayList<>();
        for (int i = 7; i >= 1; i--) {

            dates.add(date.minusDays(i - 1).toString());
            Long buyerSumAmount = buyerExecuteDetailMapper.getSumAmountBySupplierIdAndDate(supplierId, i - 1);
            Long releaseCargoSumAmount = releaseCargoMapper.getSumAmountBySupplierIdAndDate(supplierId, i - 1);
            Long qualitySumAmount = qualityDetailMapper.getSumAmountBySupplierIdAndDate(supplierId, i - 1);
            buyerSumAmounts.add(buyerSumAmount == null ? 0 : buyerSumAmount);
            releaseCargoSumAmounts.add(releaseCargoSumAmount == null ? 0 : releaseCargoSumAmount);
            qualitySumAmounts.add(qualitySumAmount == null ? 0 : qualitySumAmount);

        }

        List<Map> series = new ArrayList<>();

        Map<String, Object> buyer = new HashMap<>();
        Map<String, Object> release = new HashMap<>();
        Map<String, Object> quality = new HashMap<>();
        buyer.put("name", "采购总数");
        buyer.put("type", "line");
        buyer.put("stack", "总量");

        Map<String, Object> buyerLineStyle = new HashMap<>();
        Map<String, Object> buyerNormal = new HashMap<>();

        buyerNormal.put("color","#0717FF");
        buyerNormal.put("width",3);
        buyerLineStyle.put("normal",buyerNormal);
        buyer.put("lineStyle",buyerLineStyle);


        buyer.put("data", buyerSumAmounts);

        release.put("name", "放货总数");
        release.put("type", "line");
        release.put("stack", "总量");
        release.put("data", releaseCargoSumAmounts);

        Map<String, Object> releaseLineStyle = new HashMap<>();
        Map<String, Object> releaseNormal = new HashMap<>();

        releaseNormal.put("color","#FFF936");
        releaseNormal.put("width",3);
        releaseLineStyle.put("normal",releaseNormal);
        release.put("lineStyle",releaseLineStyle);

        quality.put("name", "合格总数");
        quality.put("type", "line");
        quality.put("stack", "总量");
        quality.put("data", qualitySumAmounts);

        Map<String, Object> qualityLineStyle = new HashMap<>();
        Map<String, Object> qualityNormal = new HashMap<>();

        qualityNormal.put("color","#33FF32");
        qualityNormal.put("width",3);
        qualityLineStyle.put("normal",qualityNormal);
        quality.put("lineStyle",qualityLineStyle);

        series.add(buyer);
        series.add(release);
        series.add(quality);

        map.put("date", dates);
        map.put("series", series);

        return ResultFactory.buildSuccessResult("查询成功", map);
    }

    @Override
    public Result readExcel(List<SupplierFile> supplierFiles) {
        List<String> allSupplierName = supplierFileMapper.getAllSupplierName();
        for (SupplierFile supplierFile : supplierFiles) {
            if (!allSupplierName.contains(supplierFile.getSupplierName())){
                supplierFile.setId(String.valueOf(idWorker.nextId()));
                supplierFileMapper.insert(supplierFile);
            }
        }
        return ResultFactory.buildSuccessResult("导入成功");
    }
}
